﻿package net.wangit.util;

import java.net.InetAddress;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerUtil {

	private static final Log log = LogFactory
			.getLog(net.wangit.util.ServerUtil.class);

	public ServerUtil() {
	}

	public static String getLocalIp() {
		try {
			return getIP(InetAddress.getLocalHost());
		} catch (Exception ex) {
			log.error("local host IP can't find !");
			return null;
		}
	}

	public static String[] getIps() {
		try {
			String result[];
			String hostName = InetAddress.getLocalHost().getHostName();
			InetAddress ips[] = InetAddress.getAllByName(hostName);
			result = new String[ips.length];
			for (int i = 0; i < ips.length; i++)
				result[i] = getIP(ips[i]);
			return result;
		} catch (Exception e) {
		}
		return null;
	}

	private static String getIP(InetAddress ia) {
		int ip[] = new int[4];
		byte ips[] = ia.getAddress();
		for (int ii = 0; ii < ip.length; ii++)
			ip[ii] = ips[ii] & 0xff;

		String ipStr = (new StringBuilder(String.valueOf(ip[0]))).append(".")
				.append(ip[1]).append(".").append(ip[2]).append(".").append(
						ip[3]).toString();
		return ipStr;
	}

}
